package cn.psh.product.service.impl;

import cn.psh.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.psh.basic.util.AjaxResult;
import cn.psh.product.domain.Product;
import cn.psh.product.domain.ProductDetail;
import cn.psh.product.domain.ProductOnlineAuditLog;
import cn.psh.product.mapper.ProductDetailMapper;
import cn.psh.product.mapper.ProductMapper;
import cn.psh.product.mapper.ProductOnlineAuditLogMapper;
import cn.psh.product.service.IProductService;
import cn.psh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pider
 * @since 2022-09-27
 */
@Transactional
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Transactional
    @Override
    public void save(Product product) {
        super.save(product);
//        获取服务详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }
    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        productMapper.delProductById(id);
    }
    @Transactional
    @Override
    public void batchDel(List<Long> ids) {
        super.batchDel(ids);
        for (Long id : ids) {
            del(id);

        }
    }

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        final ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
        productDetail.setProductId(product.getId());
        productDetailMapper.update(productDetail);
        }
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids) {
//        遍历需要审核的内容
        for (Long id : ids) {
            final Product product = productMapper.queryById(id);
//            百度Ai校验
            final Boolean aBoolean = baiduAiAuditService.textAudit(product.getName());
            final Boolean aBoolean1 = baiduAiAuditService.imageAudit(product.getResources());

            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            if(aBoolean && aBoolean1){
//                改状态为1，并且设置上架时间
//                final Product product1 = new Product();
                product.setState(1l);
                product.setOffsaletime(new Date());
                productMapper.update(product);
//                添加审核日志
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("审核通过！");
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
            }else {
//                审核失败
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote("审核失败");
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                return new AjaxResult().setSuccess(false).setMessage("图片审核不通过");
            }
        }
        return AjaxResult.OJBK().setSuccess(true);

    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.queryById(id);
            product.setState(0l);
            product.setOnsaletime(new Date());
            productMapper.update(product);
        }
        return AjaxResult.OJBK().setSuccess(true);

    }



}
